#!/usr/bin/env bash

# Copyright (c) Open Enclave SDK contributors.
# Licensed under the MIT License.

##==============================================================================

function trap_handler()
{
    echo "ERROR LINE# ${1} : ${2}"
    exit "${1}"
}

trap 'trap_handler ${LINENO} $?' ERR

builddir=$1
mode=$2

case $mode in 
    ssh)
        echo "Pushing gh-pages via ssh"
        remote="git@github.com:openenclave/openenclave.git"
    ;;
    
    https)
        echo "Pushing gh-pages via https"
        username=$3
        password=$4
        remote="https://${username}:${password}@github.com/openenclave/openenclave.git"
    ;;
    *)
        echo "Usage: $0 builddir [ ssh | https username pat ]";
        echo "  builddir:  Build directory for OpenEnclave repo"
        echo "  ssh:   Pushes doxygen generated docs to OpenEnclave's gh-pages"
        echo "         via ssh, requires ssh keys to be set up"
        echo "  https: Pushes doxygen generated docs to OpenEnclave's gh-pages"
        echo "         specify username and personal access token on cmd line"
        exit 1;
    ;;
esac 

root="$(git rev-parse --show-toplevel)"
cd "${root}/${builddir}" || exit 1

make doxygen

tmp=$(mktemp -d)

branch=gh-pages

cp -r "${root}/docs/refman/index.html" "${tmp}/"

cp -r output/share/doc/openenclave/html "${tmp}/api"

cd "$tmp" || exit 1

git init

git checkout --orphan $branch

git add -A 

git commit -m "doc deploy"

git push -f "$remote" $branch:$branch

rm -rf "$tmp"
